package opd.db.helpers;

public class Query {
	
	public final static byte SELECT_QUERY = 1;
	public final static byte INSERT_QUERY = 2;
	public final static byte UPDATE_QUERY = 3;
	public final static byte DELETE_QUERY = 4;

	private byte _queryType;
	private StringBuffer _query;    
	private StringBuffer _select;
	private StringBuffer _insert;
	private StringBuffer _update;
	private StringBuffer _delete;
	
	private StringBuffer _where;
	private StringBuffer _orderBy;
	private StringBuffer _limit;
	private StringBuffer _groupBy;
	private StringBuffer _having;
	
	private boolean _appendMode;

////////////////////////////////////////////////////////////////

public Query() {
  
		_queryType = SELECT_QUERY;
	  
	_query = new StringBuffer("");
  _select = new StringBuffer("");
  _where = new StringBuffer("");
  _orderBy = new StringBuffer("");
  _groupBy = new StringBuffer("");
		_limit = new StringBuffer("");
		_insert = new StringBuffer("");
		_update = new StringBuffer("");
		_delete = new StringBuffer("");
		_having = new StringBuffer("");
	
  _appendMode = false;
    
}//Query

//--------------------------------------------------------------

public Query(byte queryType){
	
	  _queryType = queryType;		
	  _query = new StringBuffer("");
	  _appendMode = false;

	  switch (queryType){
		  case SELECT_QUERY:
			  _select = new StringBuffer("");
			  _where = new StringBuffer("");
				_groupBy = new StringBuffer("");
			  _orderBy = new StringBuffer("");
				_having = new StringBuffer("");				 
		  break;
		  case INSERT_QUERY:
			  _insert = new StringBuffer("");
		  break;
			case UPDATE_QUERY:
				_update = new StringBuffer("");
				_where = new StringBuffer("");
			break;
			case DELETE_QUERY:
				_delete = new StringBuffer("");
				_where = new StringBuffer("");
			break;
	  }//switch
	
}// Query

////////////////////////////////////////////////////////////////

public void setAppendMode(boolean appendMode){
    _appendMode = appendMode;
}//setAppendMode

//-------------------------------------------

public boolean getAppendMode(){
    return _appendMode;
}//getAppendMode

//-------------------------------------------

	public void setQueryType(byte queryType){
		_queryType = queryType;
		
	/**
		* 2004-01-28: Marcello 
	**/
	
	switch (queryType){
		case SELECT_QUERY:
			if (_select==null){
				_select = new StringBuffer("");
			}//if
			if (_where==null){
				_where = new StringBuffer("");
			}//if
			if (_orderBy==null){
				_orderBy = new StringBuffer("");
			}//if
			if (_groupBy==null){
				_groupBy = new StringBuffer("");
			}//if
			if (_having == null){
				_having = new StringBuffer("");
			}//if
		break;
		case INSERT_QUERY:
			if (_insert==null){
				_insert = new StringBuffer("");
			}//if
		break;
		  case UPDATE_QUERY:
		  	if (_update==null){
					_update = new StringBuffer("");
		  	}//if
			  if (_where==null){
					_where = new StringBuffer(""); 
			  }//if
		  break;
		  case DELETE_QUERY:
		  	if (_delete==null){
				_delete = new StringBuffer(""); 
		  	}//if
			  if (_where==null){
				_where = new StringBuffer("");
			  }//if
			  
		  break;
	}//switch
		
  }//setQueryType

	//-------------------------------------------

	public byte getQueryType(){
		return _queryType;
	}//getQueryType
	
	//-------------------------------------------
	
public void setSelect(String select){
  if (!_appendMode){
  	_select.setLength(0);
  }//if    
    
  _select.append(select);
  
}//setSelect

//-------------------------------------------


  public void setInsert(String tableName,
											  String[] tableFields,
											  String[] values){
	  _insert.setLength(0);
	
	  _insert.append("INSERT INTO ")
					  .append(tableName)
					  .append(" (");
	  _insert.append(tableFields[0]);
	
	  for (int i=1; i<tableFields.length;i++){
		  _insert.append(",")
					   .append(tableFields[i]);			
	  }//for
	
	  _insert.append(") ");
	
	  _insert.append(" VALUES")
			 .append(" (");
	
	  _insert.append(values[0]);
	
	  for (int i=1; i<values.length;i++){
		  _insert.append(",")
					   .append(values[i]);			
	  }//for
	  _insert.append(") ");
	
  }//setInsert
  	
//-------------------------------------------

public void setUpdate(String tableName,
											String[] fields,
											String[] values){
	
	_update.setLength(0);
	
	_update.append("UPDATE ")
				 .append(tableName)
				 .append(" SET ");
	
	_update.append(fields[0])
		   .append("=")
		   .append(values[0]);
	
	for (int i=1; i<fields.length; i++){
		_update.append(",")
			   .append(fields[i])
			   .append("=")
			   .append(values[i]);
	}//for
	
}//setUpdate

//-------------------------------------------

public void setUpdate(String tableName,
											String field,
											String value){
												
	_update.setLength(0);
	
	_update.append("UPDATE ")
				 .append(tableName)
				 .append(" SET ")
		   .append(field)
			 .append("=")
			   .append(value);
				   
}//setUpdate

//-------------------------------------------

public void setUpdate(String tableName,
											String statement){
	
	_update.setLength(0);
	
	_update.append("UPDATE ")
				 .append(tableName)
				 .append(" SET ")
				 .append(statement);					 

}//setUpdate

//-------------------------------------------

public void setDelete(String tableName){
	
	_delete.setLength(0);
	
	_delete.append("DELETE FROM ")
		   .append(tableName);
	
}//setDelete

//------------------------------------------- 

public void setWhere(String where){
    
    if (!_appendMode){
        _where.setLength(0);
    }//if
    
    _where.append(where);
    
}//setWhere

//-------------------------------------------

public void setHaving(String having){
	_having.append(having);
}//setHaving

//-------------------------------------------

public void setWhere(String compination,
                     String[] expressions){
                         
  StringBuffer w = new StringBuffer("");
    
		w.append(expressions[0]);		 
	 
  for (int i=1; i<expressions.length; i++){
  	w.append(" ").append(compination).append(" ");
    w.append(expressions[i]);
		}//for
    
  setWhere(" (" + w.toString() + ") ");
    
}//setWhere

//-------------------------------------------
/**
* Only the tokens "IN" AND "BETWEEN" are supported.
* */
public void setWhere(String field,
                    String token,
                    String[] expressions){
                   	
StringBuffer w = new StringBuffer("");
	
	if (token.toUpperCase().equals("IN")){
        w.append(field);
        w.append(" ");
        w.append(token);
        w.append("(");
        w.append(expressions[0]);
        for (int i=1; i<expressions.length; i++){
            w.append(",").append(expressions[i]);
        }//for
        w.append(")");
		
        setWhere(" (" + w.toString() + ") ");
		
        return;
	}//if IN
	
	//-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
	
if (token.toUpperCase().equals("BETWEEN")){
        if (expressions.length==2){
            w.append(field)
             .append(" ")
         .append(token)
	 .append(" '").append(expressions[0]).append("'")
	 .append(" AND ")
	 .append("'").append(expressions[1]).append("'");
			 
	 setWhere(" (" + w.toString() + ") ");				 
    }//if
		
        return;
}//if BETWEEN

}//setWhere

//-------------------------------------------

public void setOrderBy(String orderBy){
   
   if (!_appendMode){
       _orderBy.setLength(0);
   }//if
   
   _orderBy.append(orderBy);
   
}//setOrderBy

//-------------------------------------------

public void setGroupBy(String groupBy){
   
   if (!_appendMode){
	   _groupBy.setLength(0);
   }//if
   
	_groupBy.append(groupBy);
   
}//setOrderBy

//-------------------------------------------

public void setLimit(String startRecord, 
                    String recordsCount){
	
	if (!_appendMode){
		_limit.setLength(0);
	}//if
	
	_limit.append(startRecord)
			.append(",")
				.append(recordsCount);
				 
}//setLimit

// -------------------------------------------

public String getSelect(){
    return _select.toString();
}//getSelect

// -------------------------------------------
public String getWere(){
return _where.toString();
}//getWere

//-------------------------------------------

public String getOrderBy(){
return _orderBy.toString();
}//getOrderBy

//-------------------------------------------
public String getGroupBy(){
		return _groupBy.toString();
}//getGroupBy

//-------------------------------------------

public String getLimit(){
		return _limit.toString();
}//getLimit
 //-------------------------------------------
 
public String getQuery(){
	_query.setLength(0);
		
	switch(_queryType){
		case SELECT_QUERY:
			if (_select.length()>0){
				_query.append(_select);
			}//if
			
			if (_where.length()>0){
				_query.append(" WHERE " + _where);
			}//if
   
   	if (_groupBy.length()>0){
   		_query.append(" GROUP BY " + _groupBy);
   		if (_having.length()>0){
   			_query.append(" HAVING " + _having);
   		}//if
   	}//if
   	
			if (_orderBy.length()>0){
				_query.append(" ORDER BY " + _orderBy);
			}//if				
		break;
		case INSERT_QUERY:
			_query.append(_insert);
		break;
		case UPDATE_QUERY:
			if (_update.length()>0){
				_query.append(_update);
			}//if
			
			if (_where.length()>0){
				_query.append(" WHERE " + _where);
			}//if
		break;
		case DELETE_QUERY:
			if (_delete.length()>0){
				_query.append(_delete);
			}//if
			
		if (_where.length()>0){
			_query.append(" WHERE " + _where);
		}//if
		break;
	}//switch
			  
	return _query.toString();

}//getQuery

}//Query
